/** \file Chat.cpp
 *  \author Adrian Schweizer
 *  \created  $Sa 18 Aug 03:48:01 pm CEST 2007 schwadri@SchwadriComp.local$
 *  \modified $So 19 Aug 02:54:14 pm CEST 2007 schwadri@SchwadriComp.local$
 */

#include "Chat.hpp"

using namespace game;

//===============================================================================
///Chat Implementation
//===============================================================================

Chat::Chat()
//:   m_channels(CHANNEL_VALID_END,0)   //reserve space for the channels
{
    /*try {
        for(channel_iterator chiter=m_channels.begin();chiter!=m_channels.end();++chiter)
            *chiter = new Channel;
    }
    catch(...)
    {
        //clean up
        for(channel_iterator chiter=m_channels.begin();chiter!=m_channels.end();++chiter)
            delete *chiter;
        throw;
    }*/
}

void Chat::PushBack(const Message& m, char channel)
{

    if(channel!=CHANNEL_INVALID)
    {
        if(channel != CHANNEL_ALL)
            m_channels[channel].PushBack(m);
    }
    else
    {
        //put in CHANNEL_ALL
        m_channels[CHANNEL_ALL].PushBack(m);
        if(m.Channel()!=CHANNEL_ALL)
            m_channels[m.Channel()].PushBack(m);
    }
}

void Chat::Clear()
{
    foreach(Channel & ch, m_channels)
        ch.Clear();
}

//===============================================================================
///Chat::Channel Implementation
//===============================================================================

void Chat::Channel::PushBack(const Message& m)
{
    m_messages.push_back(m);
    ChangedSignal();
}


void Chat::Channel::Clear()
{
    m_messages.clear();
    ChangedSignal();
}
